<?php
global $redis;
$redis = new redis();
$result = $redis->connect('127.0.0.1', 6379);



session("name");

var_dump(session("name")) ;


function session($name,$val){
    global $redis;
    $data=$redis->GET(getsessionid());
    if(!$data){
        $data=[];
    }
    else{
        $data=json_decode($data,true);
    }

    //获取session
    if(empty($val)){
        if(empty($data[$name])){
            return null;
        }
        else{
            return $data[$name];
        }
        //return $redis->GET(getsessionid());
    }
    //删除session
    else if(is_null($val)){
        unset($data[$name]);
        $redis->SETEX(getsessionid(), 3600,json_encode($data));
    }
    //设置session
    else{
        $data[$name]=$val;
        $redis->SETEX(getsessionid(), 3600,json_encode($data));
    }
}

function getsessionid(){

    if(empty($_COOKIE['session_id'])){
        $session_id=uniqid();
        setcookie("session_id",$session_id,0);
    }
    else{
        $session_id=$_COOKIE['session_id'];
    }
    return $session_id;
}